﻿using RabbitMQ.Client.Events;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuanYijia.Application.ProductionManagement
{
    public class RecevieNew
    {
        public static async Task SimpleConsumerAsync()
        {
            string queueName = "simple_order";



            using (var connection = await RabbitMQHelperNew.GetConnectionAsync())
            using (var channel = await connection.CreateChannelAsync())
            {
                // 创建队列
                await channel.QueueDeclareAsync(queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);

                // 创建消费者
                var consumer = new AsyncEventingBasicConsumer(channel);

                int i = 0;

                // 消费者事件处理
                consumer.ReceivedAsync += async (model, ea) =>
                {
                    var message = Encoding.UTF8.GetString(ea.Body.ToArray());
                    Console.WriteLine($"{i}, 队列 {queueName} 消费消息长度: {message.Length}");
                    i++;
                    await Task.CompletedTask; // 异步消费消息的标志
                };

                // 开始消费
                await channel.BasicConsumeAsync(queueName, autoAck: true, consumer: consumer);

                // 等待消费者执行，避免主线程过早退出
                Console.WriteLine("消费者已启动，按任意键退出...");
                Console.ReadKey();
            }
        }
    }
}
